package com.czy.owner.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.PrePayBalancePaymentModel;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by King on 2017-03-15
 */

public class PickCouponAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<PrePayBalancePaymentModel.CouponsListBean> couponData = new ArrayList<>();
    private List<PrePayBalancePaymentModel.CouponsListBean> discountData = new ArrayList<>();
    private List<PrePayBalancePaymentModel.CouponsListBean> cashData = new ArrayList<>();
    private int cashColor = Color.parseColor("#FEA454"), disenbledColor = Color.parseColor("#EBEBEB");
    private boolean allCoupons = false;
    private double tailAmount = 0;

    public PickCouponAdapter(Context mContext, List<PrePayBalancePaymentModel.CouponsListBean> couponData, boolean allCoupons, double tailAmount) {
        this.mContext = mContext;
        this.couponData = couponData;
        this.allCoupons = allCoupons;
        this.tailAmount = tailAmount;
        for (int i = 0; i < couponData.size(); i++) {
            if (couponData.get(i).getCouponsType().equals("discountVoucher")) {
                discountData.add(couponData.get(i));
            } else if (couponData.get(i).getCouponsType().equals("cashVoucher")) {
                cashData.add(couponData.get(i));
            }
        }
    }

    /**
     * 根据组位置获取对应的队列
     *
     * @param groupPosition
     * @return
     */
    private List<PrePayBalancePaymentModel.CouponsListBean> getGroupData(int groupPosition) {
        if (groupPosition == 0 && discountData.size() > 0) {
            return discountData;
        } else {
            return cashData;
        }
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return getGroupData(groupPosition).size();
    }

    @Override
    public int getGroupCount() {
        return (discountData.size() > 0 ? 1 : 0) + (cashData.size() > 0 ? 1 : 0);
    }

    @Override
    public PrePayBalancePaymentModel.CouponsListBean getGroup(int groupPosition) {
        return null;
    }

    @Override
    public PrePayBalancePaymentModel.CouponsListBean getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder = null;
        if (convertView == null) {
            groupViewHolder = new GroupViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pick_coupon_header, parent, false);
            groupViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_item_coupon_title);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        if (groupPosition == 0 && discountData.size() > 0) {
            groupViewHolder.tvTitle.setText("已有折扣券");
        } else {
            groupViewHolder.tvTitle.setText("已有现金券");
        }
        groupViewHolder.tvTitle.setTag(groupPosition);
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChildViewHolder childViewHolder;
        if (convertView == null) {
            childViewHolder = new ChildViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pick_coupon2, parent, false);
            childViewHolder.cbPicked = (CheckBox) convertView.findViewById(R.id.cb_item_picked);
            childViewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_item_coupon_name);
            childViewHolder.llTimeWrapper = (LinearLayout) convertView.findViewById(R.id.ll_item_time_wrapper);
            childViewHolder.tvTime = (TextView) convertView.findViewById(R.id.tv_item_time);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        if (getGroupData(groupPosition).get(childPosition).getMinimum() > tailAmount) {
            childViewHolder.cbPicked.setEnabled(false);
            childViewHolder.cbPicked.setVisibility(View.GONE);
        } else {
            childViewHolder.cbPicked.setEnabled(true);
            childViewHolder.cbPicked.setVisibility(View.VISIBLE);
        }
        childViewHolder.cbPicked.setTag(groupPosition + "," + childPosition);
        childViewHolder.cbPicked.setChecked(getGroupData(groupPosition).get(childPosition).isPicked());

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getGroupData(groupPosition).get(childPosition).getMinimum() > tailAmount){
                    return;
                }
                childViewHolder.cbPicked.performClick();
            }
        });



        childViewHolder.cbPicked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (allCoupons) {
                    for (int i = 0; i < getGroupData(groupPosition).size(); i++) {
                        getGroupData(groupPosition).get(i).setPicked(false);
                    }
                } else {
                    for (int i = 0; i < discountData.size(); i++) {
                        discountData.get(i).setPicked(false);
                    }
                    for (int i = 0; i < cashData.size(); i++) {
                        cashData.get(i).setPicked(false);
                    }
                }
                getGroupData(groupPosition).get(childPosition).setPicked(((CheckBox) v).isChecked());
                notifyDataSetChanged();
            }
        });



        if (groupPosition == 0 && discountData.size() > 0) {
            childViewHolder.tvName.setText("满" + getGroupData((groupPosition)).get(childPosition).getMinimum() + "元打" + getGroupData((groupPosition)).get(childPosition).getDisCount()*10 + "折");
        } else {
            childViewHolder.tvName.setText("满" + getGroupData((groupPosition)).get(childPosition).getMinimum() + "元减" + getGroupData((groupPosition)).get(childPosition).getDenomination()+"元");
        }


        childViewHolder.tvTime.setText("到期时间：" + formatTime(getGroupData((groupPosition)).get(childPosition).getEndTime()));
        if (getGroupData(groupPosition).get(childPosition).getMinimum() > tailAmount) {
            childViewHolder.tvName.setTextColor(disenbledColor);
            childViewHolder.llTimeWrapper.setBackgroundColor(disenbledColor);
        } else if (groupPosition == 0 && discountData.size() > 0) {
            childViewHolder.tvName.setTextColor(ContextCompat.getColor(mContext, R.color.appThemeColor));
            childViewHolder.llTimeWrapper.setBackgroundResource(R.color.appThemeColor);
        } else {
            childViewHolder.tvName.setTextColor(cashColor);
            childViewHolder.llTimeWrapper.setBackgroundColor(cashColor);
        }

        return convertView;
    }

    public String formatTime(long timeStamp) {
        if (timeStamp == 0) {
            return "无限制";
        }
        String formatDate = "yyyy年MM月dd日";
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatDate);
        String tmpDateStr = dateFormat.format(timeStamp);
        return tmpDateStr;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }


    class GroupViewHolder {
        TextView tvTitle;
    }

    class ChildViewHolder {
        CheckBox cbPicked;
        TextView tvName;
        LinearLayout llTimeWrapper;
        TextView tvTime;
    }
}
